package com.zyf.connotation.model

import android.util.Log
import com.google.gson.Gson
import com.zyf.connotation.utils.RequestMap
import com.zyf.rxjavaapp.utils.ServiceUtils
import com.zyf.rxjavaapp.webinterface.WebService
import org.json.JSONObject
import retrofit2.Call
import rx.Observable

/**
 * 作者：Think
 * 创建于 2017/5/13 08:41
 */
class ConnotationModel:IConnotationModel{
    var gson = Gson()
    var service = ServiceUtils.createService(WebService::class.java)
    override fun <T> netDatat(t: T, call:Call<String>): Observable<T> {
        return Observable.create({
            var str = call?.execute()?.body()
            Log.e("123",str)
            var json = JSONObject(str)
            if (json.get("message").equals("success")) {
                it.onNext(gson.fromJson(json.getString("data"), t as Class<T>))
            }
            it.onCompleted()
        })
    }

    override fun netFabulous(check:String,id:String):Observable<String> {
        var request = RequestMap.getMap()
        request.put("action",check)
        request.put("group_id",id)
        request.put("item_id",id)
        val call = service.fabulous(request)
        return Observable.create({
            var str = call?.execute()?.body()
            var json = JSONObject(str)
            if(json.get("message").equals("success")){
                if(check.equals("digg")){
                    it.onNext(json.getString("digg_count"))
                }else{
                    it.onNext(json.getString("bury_count"))
                }
            }
            it.onCompleted()
        })
    }

    override fun <T> DiggUser(t: T,id:String): Observable<T> {
        val call = service.getDiggUser(id)
        return Observable.create({
            var str = call?.execute()?.body()
            var json = JSONObject(str)
            if (json.get("message").equals("success")) {
                it.onNext(gson.fromJson(json.getString("data"), t as Class<T>))
            }
            it.onCompleted()
        })
    }
}